//
//  ReachabilitySource.swift
//  OneDay
//
//  Created by xicheng on 16/11/14.
//  Copyright © 2016年 xicheng. All rights reserved.
//

import UIKit

enum ReachabilityType {
  
  case reachable // 已经连接
  
  case mobile // 移动网络
  
  case wifi  // wifi

  case error // 连接失败
}


protocol ReachabilityEvent:NSObjectProtocol {

  func handlerReachabilityEvent(type:ReachabilityType)
}


class ReachabilitySource: NSObject {

  private static var eventList = [ReachabilityEvent]()
		
  
  static func addEvent(_ event:ReachabilityEvent){
    self.eventList.append(event)
  }
  
  static func removeEvent(_ event:ReachabilityEvent){
    
    
    let count = eventList.count
    for index in 0  ..< count {
      if event.isEqual(eventList[index]) {
        self.eventList.remove(at: index)
        return
      }
    }
  }
  
  static func removeAllEvent(){
    self.eventList.removeAll()
  }
  
  static func notifyAll(type:ReachabilityType){
    
    guard self.eventList.count > 0 else {
      return
    }
    
    for event in self.eventList {
      event.handlerReachabilityEvent(type: type)
    }
    
  }
  
  
}
